﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using YYS;

namespace WindowsService
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }
        private Timer timer;
        protected override void OnStart(string[] args)
        {
            timer = new Timer();
            timer.Interval = 24 * 60 * 60 * 1000; // 一天的毫秒数
            timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
            timer.Start();
        }
        private void OnElapsedTime(object source, ElapsedEventArgs e)
        {
            // 确定是否为每天的8点
            if (e.SignalTime.Hour == 20 && e.SignalTime.Minute == 0 && e.SignalTime.Second == 0)
            {
                // 执行你的任务
                ExecuteTask();
            }
        }
        private void WriteLog(string message)
        {
            // 获取当前执行的程序集所在的路径
            string assemblyLocation = Assembly.GetExecutingAssembly().Location;
            // 获取这个路径的目录部分，即项目根目录
            string directory = Path.GetDirectoryName(assemblyLocation);

            using (System.IO.StreamWriter sw=new System.IO.StreamWriter(directory + @"\log.log", true))
            {
                sw.WriteLine(message);
            }               
        }
        class user
        {
            public string openId { get; set; }
            public string schMark { get; set; }
            public string schDate { get; set; }
            public string schTitle { get; set; }
        }
        private void ExecuteTask()
        {
            WriteLog("ExecuteTask");
            var list = new List<user>();
            // 你的任务逻辑
            #region
            var db = Helper.GETdbHelper(dbHelper.EnumServer.Local, dbHelper.ExecType.WhileRead, DefaultValue.DataBase);
            db.CmdText = "PR_PRC_ScheduleSend";
            db.IsProc = true;
            db.NeedOutError = true;
            db.FuncRead = sdr =>
            {
                list.Add(sdr.xTo<user>());
            };
            db.Exec();
            if (db.HasError) WriteLog("OnError...Message = {0}".xFormat(db.Error));
            WriteLog("db... = {0}".xFormat(list.xConvertObjectToJson(false)));
            var data=YYS.Controllers.wxController.wxScheduleSend_win(list.xConvertObjectToJson(false), "");
            if(data.HasError) WriteLog("出错了："+data.Error);
            foreach (var x in data.ObjList)
            {
                WriteLog("SyncResult...openId = {0}, templateId = {1}, uiName = {2},errmsg={3}".xFormat(x.openId, x.templateId, x.name, x.errmsg));
            }
            //foreach (var x in list)
            //{
            //    Console.WriteLine("SyncResult...openId = {0}, schTitle = {1}, schDate = {2},schMark={3}".xFormat(x.openId, x.schTitle, x.schDate, x.schMark));
            //    WriteLog("SyncResult...openId = {0}, schTitle = {1}, schDate = {2},schMark={3}".xFormat(x.openId, x.schTitle, x.schDate,x.schMark));
            //}
            #endregion
        }

        protected override void OnStop()
        {
            if (timer != null)
            {
                timer.Stop();
                timer.Dispose();
            }
        }
    }
}
